package com.zhsm.fuxi.mapmanager;

import android.content.Context;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.TextureMapView;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.utils.CoordinateConverter;
import com.zhsm.fuxi.mapmanager.MapStrategy.BaiduMapStrategy;
import com.zhsm.fuxi.mapmanager.MapStrategy.BaseMapLoaderStrategy;
import com.zhsm.fuxi.mapmanager.MapStrategy.LocationCallBack;
import com.zhsm.fuxi.mapmanager.MapStrategy.MyOrientationListener;
import com.zhsm.fuxi.mapmanager.MapStrategy.OnOrientationListener;
import com.zhsm.fuxi.mapmanager.mark.MarkConfig;


public class MapLoader implements BaseMapLoaderStrategy {
    private static final String TAG = "MapLoader";
    private static MapLoader instance = new MapLoader();
    private BaseMapLoaderStrategy mStrategy;
    private MyOrientationListener mMyOrientationListener;
    private Boolean showMyLocation = false;
    private Context mContext;

    public static MapLoader getInstance() {
        return instance;
    }

    /**
     * 初始化Strategy
     */
    public void init() {
        if (mStrategy == null) {
            //这里也可以手动创建高德的
            this.mStrategy = new BaiduMapStrategy();
        }
    }

    @Override
    public void recycle() {
        if (mStrategy == null) {
            return;
        }
        mStrategy.recycle();
    }

    @Override
    public void  initLocation(Context context) {
        this.mStrategy.initLocation(context);
        mContext = context;

    }

    @Override
    public void startLocation(LocationCallBack callBack) {
        this.mStrategy.startLocation(callBack);

    }

    public void showMyLocation(OnOrientationListener onOrientationListener) {
        mMyOrientationListener = new MyOrientationListener(mContext);
        showMyLocation = true;
        mMyOrientationListener.start(onOrientationListener);
    }

    @Override
    public void stopLocation() {
        this.mStrategy.stopLocation();
        if (showMyLocation)
            mMyOrientationListener.stop();
    }

    @Override
    public Object addMarkBitmap(TextureMapView mapView, MarkConfig config) {
        return this.mStrategy.addMarkBitmap(mapView, config);
    }

    @Override
    public Object addMarkView(TextureMapView mapView, MarkConfig config) {
        return this.mStrategy.addMarkView(mapView, config);
    }

    @Override
    public Object getMark(TextureMapView mapView, MarkConfig config) {
        return mStrategy.getMark(mapView, config);
    }

    @Override
    public void setLocationDirectationMarker(LatLng location, float direction, BaiduMap baiduMap) {
        mStrategy.setLocationDirectationMarker(location, direction, baiduMap);
    }

    public LatLng gps2baiduLocation(LatLng sourceLatLng) {
        // 将GPS设备采集的原始GPS坐标转换成百度坐标
        CoordinateConverter converter = new CoordinateConverter();
        converter.from(CoordinateConverter.CoordType.GPS);
// sourceLatLng待转换坐标
        converter.coord(sourceLatLng);
        return converter.convert();
    }

}


